﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public partial class FormdrawVerticalPipe : System.Windows.Forms.Form
	{
		public FormdrawVerticalPipe(Autodesk.Revit.DB.Document doc, List<string> levelUp, List<string> pipeType, List<string> pipingSystemType)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.CenterToScreen();
			this.doc = doc;
			this.levelUp = levelUp;
			this.pipeType = pipeType;
			this.pipingSystemType = pipingSystemType;
			base.Icon = Resource.YArchitechIcon;
			this.osf = new OperateSegmentFitting(doc);
		}

		public FormdrawVerticalPipe(Autodesk.Revit.DB.Document doc, List<string> levelUp, List<string> pipeType, List<string> pipingSystemType, Pipe pipe)
		{
			this.InitializeComponent();
			base.CenterToScreen();
			this.doc = doc;
			this.levelUp = levelUp;
			this.pipeType = pipeType;
			this.pipingSystemType = pipingSystemType;
			this.pipe = pipe;
			base.Icon = Resource.YArchitechIcon;
			this.osf = new OperateSegmentFitting(doc);
		}

		private void buttonEsc_Click_1(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void GetUserData()
		{
			string[] riserPipeText = this.userSetting.GetRiserPipeText();
			this.textBoxPipeDiameter.Text = riserPipeText[2];
			this.comboBox_DN.Text = riserPipeText[2];
			this.textBoxOffsetDown.Text = riserPipeText[3];
			this.textBoxOffsetUp.Text = riserPipeText[4];
			this.comboBoxPipeClass.Text = riserPipeText[0];
			this.comboBoxPipingSystemType.Text = riserPipeText[1];
			this.comboBox1.SelectedItem = riserPipeText[6];
			this.comboBox2.SelectedItem = riserPipeText[7];
			this.lastLevel1 = riserPipeText[6];
			this.lastLevel2 = riserPipeText[7];
			if (riserPipeText[5] == "11")
			{
				this.checkBox1.Checked = true;
				this.checkBox2.Checked = true;
				this.label2.Text = "相对标高(m)";
				this.comboBox1.Enabled = true;
				this.label4.Text = "相对标高(m)";
				this.comboBox2.Enabled = true;
			}
			else if (riserPipeText[5] == "00")
			{
				this.checkBox1.Checked = false;
				this.checkBox2.Checked = false;
				this.label2.Text = "绝对标高(m)";
				this.comboBox1.Enabled = false;
				this.label4.Text = "绝对标高(m)";
				this.comboBox2.Enabled = false;
			}
			else if (riserPipeText[5] == "01")
			{
				this.checkBox1.Checked = false;
				this.checkBox2.Checked = true;
				this.label2.Text = "绝对标高(m)";
				this.comboBox1.Enabled = false;
				this.label4.Text = "相对标高(m)";
				this.comboBox2.Enabled = true;
			}
			else if (riserPipeText[5] == "10")
			{
				this.checkBox1.Checked = true;
				this.checkBox2.Checked = false;
				this.label2.Text = "相对标高(m)";
				this.comboBox1.Enabled = true;
				this.label4.Text = "绝对标高(m)";
				this.comboBox2.Enabled = false;
			}
			if (string.IsNullOrEmpty(this.comboBoxPipeClass.Text) && this.comboBoxPipeClass.Items != null && this.comboBoxPipeClass.Items.Count > 0)
			{
				this.comboBoxPipeClass.SelectedItem = this.comboBoxPipeClass.Items[0];
			}
			if (string.IsNullOrEmpty(this.comboBoxPipingSystemType.Text) && this.comboBoxPipingSystemType.Items != null && this.comboBoxPipingSystemType.Items.Count > 0)
			{
				this.comboBoxPipingSystemType.SelectedItem = this.comboBoxPipingSystemType.Items[0];
			}
			if (string.IsNullOrEmpty(this.comboBox1.Text))
			{
				this.lastLevel1 = this.comboBox1.Items[0].ToString();
				if (this.comboBox1.Items != null && this.comboBox1.Items.Count > 0)
				{
					this.comboBox1.SelectedItem = this.comboBox1.Items[0];
				}
			}
			if (string.IsNullOrEmpty(this.comboBox2.Text))
			{
				this.lastLevel2 = this.comboBox2.Items[0].ToString();
				if (this.comboBox2.Items != null && this.comboBox2.Items.Count > 0)
				{
					this.comboBox2.SelectedItem = this.comboBox2.Items[0];
				}
			}
		}

		private void SetUserData()
		{
			string[] array = new string[8];
			array[2] = this.comboBox_DN.Text;
			array[3] = this.textBoxOffsetDown.Text;
			array[4] = this.textBoxOffsetUp.Text;
			array[0] = this.comboBoxPipeClass.Text;
			array[1] = this.comboBoxPipingSystemType.Text;
			if (this.checkBox1.Checked && this.checkBox2.Checked)
			{
				array[5] = "11";
			}
			else if (this.checkBox1.Checked && !this.checkBox2.Checked)
			{
				array[5] = "10";
			}
			else if (!this.checkBox1.Checked && this.checkBox2.Checked)
			{
				array[5] = "01";
			}
			else if (!this.checkBox1.Checked && !this.checkBox2.Checked)
			{
				array[5] = "00";
			}
			array[6] = this.comboBox1.Text;
			array[7] = this.comboBox2.Text;
			this.userSetting.WriteRiserPipeText(array);
		}

		private double getLevelOffset(string level)
		{
			return this.Get_Level(this.doc, level).ProjectElevation;
		}

		private void buttonDraw_Click(object sender, EventArgs e)
		{
			if (this.pipe == null)
			{
				if (!Regex.IsMatch(this.comboBox_DN.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
				{
					MessageBox.Show("直径必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
					this.comboBox_DN.Focus();
					return;
				}
			}
			else if (!Regex.IsMatch(this.comboBox_DN.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				MessageBox.Show("直径必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				this.comboBox_DN.Focus();
				return;
			}
			if (!Regex.IsMatch(this.textBoxOffsetDown.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				MessageBox.Show("标高必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				this.textBoxOffsetDown.Focus();
				return;
			}
			if (!Regex.IsMatch(this.textBoxOffsetUp.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				MessageBox.Show("标高必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				this.textBoxOffsetUp.Focus();
				return;
			}
			if (!Regex.IsMatch(this.textBoxLabel.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				MessageBox.Show("标记必须为正整数", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				this.textBoxLabel.Focus();
				return;
			}
			string text;
			if (this.pipe == null)
			{
				text = this.comboBox_DN.Text;
			}
			else
			{
				text = this.comboBox_DN.Text;
			}
			if (!(this.textBoxOffsetDown.Text != "") || !(this.textBoxOffsetUp.Text != "") || !(text != ""))
			{
				MessageBox.Show("偏移量和直径都不能为空", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return;
			}
			if (double.Parse(text) > 5.0 && double.Parse(text) < 5000.0)
			{
				double num = 0.0;
				double num2 = 0.0;
				if (this.checkBox1.Checked)
				{
					num = this.zCoord(double.Parse(this.textBoxOffsetDown.Text)) + this.getLevelOffset(this.comboBox1.Text);
				}
				else if (!this.checkBox1.Checked)
				{
					num = this.zCoord(double.Parse(this.textBoxOffsetDown.Text));
				}
				if (this.checkBox2.Checked)
				{
					num2 = this.zCoord(double.Parse(this.textBoxOffsetUp.Text)) + this.getLevelOffset(this.comboBox2.Text);
				}
				else if (!this.checkBox2.Checked)
				{
					num2 = this.zCoord(double.Parse(this.textBoxOffsetUp.Text));
				}
				PipeType pipeType = this.Get_PipeType(this.doc, this.comboBoxPipeClass.Text);
				PipingSystemType pipingSystemType = this.Get_PipingSystemType(this.doc, this.comboBoxPipingSystemType.Text);
				this.zStart = num;
				this.zEnd = num2;
				if (Math.Abs(num - num2) * 0.3048 > 0.01)
				{
					this.pipeTypeRiser = pipeType;
					this.pipingSystemTypeRiser = pipingSystemType;
					this.diameter = text;
					this.labelPipe = this.textBoxLabel.Text;
					if (this.pipe == null)
					{
						this.SetUserData();
					}
					base.DialogResult = DialogResult.OK;
					return;
				}
				MessageBox.Show("立管起始点相距太近，不能小于0.01米", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return;
			}
			else
			{
				MessageBox.Show("直径应大于5，小于5000", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				if (this.pipe == null)
				{
					this.comboBox_DN.Focus();
					return;
				}
				this.comboBox_DN.Focus();
				return;
			}
		}

		private void comboBoxPipeClass_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.isIni)
			{
				this.comboBox_DN.Items.Clear();
				foreach (MEPSize mepsize in this.osf.GetPipeSegmentByPipeTypeName(this.comboBoxPipeClass.Text).GetSizes())
				{
					this.comboBox_DN.Items.Add(Math.Ceiling(304.7 * mepsize.NominalDiameter).ToString());
				}
			}
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			foreach (string item in this.pipingSystemType)
			{
				this.comboBoxPipingSystemType.Items.Add(item);
			}
			foreach (string item2 in this.levelUp)
			{
				this.comboBox1.Items.Add(item2);
				this.comboBox2.Items.Add(item2);
			}
			foreach (string item3 in this.pipeType)
			{
				this.comboBoxPipeClass.Items.Add(item3);
			}
			if (this.pipe == null)
			{
				this.textBoxLabel.Text = this.Get_MaxPipeLabel(this.doc);
				this.GetUserData();
				this.comboBox_DN.Items.Clear();
				IEnumerator<MEPSize> enumerator2 = this.osf.GetPipeSegmentByPipeTypeName(this.comboBoxPipeClass.Text).GetSizes().GetEnumerator();
				{
					while (enumerator2.MoveNext())
					{
						MEPSize mepsize = enumerator2.Current;
						this.comboBox_DN.Items.Add(Math.Ceiling(304.7 * mepsize.NominalDiameter).ToString());
					}
					goto IL_425;
				}
			}
			if (this.pipe != null)
			{
				this.Text = "立管参数修改";
				this.buttonDraw.Text = "修改";
				this.label2.Text = "相对标高(m)";
				this.label4.Text = "相对标高(m)";
				this.comboBoxPipingSystemType.Text = this.doc.GetElementById(this.pipe.GetParameter(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM).AsElementId()).Name;
				this.comboBoxPipeClass.Text = this.pipe.PipeType.Name;
				Level level = this.pipe.Document.GetElementById(this.pipe.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsElementId()) as Level;
				if (level == null)
				{
					level = YJKRevitTools.GetLevelClose((this.pipe.Location as LocationCurve).Curve.GetEndPoint(0), this.doc);
				}
				this.getLevelOffset(level.Name);
				this.textBoxOffsetDown.Text = (double.Parse(this.pipe.GetParameter(BuiltInParameter.RBS_START_OFFSET_PARAM).AsValueString()) / 1000.0).ToString();
				this.textBoxOffsetUp.Text = (double.Parse(this.pipe.GetParameter(BuiltInParameter.RBS_END_OFFSET_PARAM).AsValueString()) / 1000.0).ToString();
				this.textBoxLabel.Text = this.pipe.GetParameter(BuiltInParameter.DOOR_NUMBER).AsString();
				this.comboBox1.SelectedItem = level.Name;
				this.comboBox2.SelectedItem = level.Name;
				this.checkBox1.Checked = true;
				this.checkBox2.Checked = true;
				this.lastLevel1 = this.comboBox1.Text;
				this.lastLevel2 = this.comboBox2.Text;
				this.comboBox_DN.Items.Clear();
				foreach (MEPSize mepsize2 in (this.doc.GetElementById(this.pipe.GetParameter(BuiltInParameter.RBS_PIPE_SEGMENT_PARAM).AsElementId()) as PipeSegment).GetSizes())
				{
					this.comboBox_DN.Items.Add(Math.Ceiling(304.7 * mepsize2.NominalDiameter).ToString());
				}
				this.comboBox_DN.Text = (this.pipe.Diameter * 304.8).ToString();
			}
			IL_425:
			this.isIni = true;
		}

		private double zCoord(double offset)
		{
			return offset / 0.3048;
		}

		private static bool IsNum(string str)
		{
			if (!string.IsNullOrEmpty(str))
			{
				for (int i = 0; i < str.Length; i++)
				{
					if (str[i] < '0' || str[i] > '9')
					{
						return false;
					}
				}
				return true;
			}
			return false;
		}

		private string Get_MaxPipeLabel(Autodesk.Revit.DB.Document document)
		{
			IEnumerable<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(Pipe), document);
			List<int> list = new List<int>();
			list.Add(0);
			foreach (Element element in elementByClass)
			{
				if (FormdrawVerticalPipe.IsNum((element as Pipe).GetParameter(BuiltInParameter.DOOR_NUMBER).AsString()))
				{
					list.Add(int.Parse((element as Pipe).GetParameter(BuiltInParameter.DOOR_NUMBER).AsString()));
				}
			}
			return (list.Max() + 1).ToString();
		}

		private PipeType Get_PipeType(Autodesk.Revit.DB.Document document, string pipeTypeName)
		{
			ICollection<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(PipeType), document);
			return this.GetElementSameName(elementByClass, pipeTypeName) as PipeType;
		}

		private PipingSystemType Get_PipingSystemType(Autodesk.Revit.DB.Document document, string pipingSystemTypeName)
		{
			ICollection<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(PipingSystemType), document);
			return this.GetElementSameName(elementByClass, pipingSystemTypeName) as PipingSystemType;
		}

		private Level Get_Level(Autodesk.Revit.DB.Document document, string pipingSystemTypeName)
		{
			ICollection<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(Level), document);
			return this.GetElementSameName(elementByClass, pipingSystemTypeName) as Level;
		}

		private Element GetElementSameName(ICollection<Element> collection, string elemName)
		{
			Element result = null;
			foreach (Element element in collection)
			{
				if (element != null && element.Name == elemName)
				{
					result = element;
				}
			}
			return result;
		}

		private void textBoxOffsetDown_TextChanged(object sender, EventArgs e)
		{
		}

		private void textBoxLabel_Validated(object sender, EventArgs e)
		{
			if (!new Regex("^[0-9]*$").IsMatch(this.textBoxLabel.Text))
			{
				YJKMessageBox.Show("标记请输入数字！");
				this.textBoxLabel.Focus();
				this.textBoxLabel.SelectAll();
				return;
			}
		}

		private void radioButton1_CheckedChanged(object sender, EventArgs e)
		{
		}

		private void radioButton2_CheckedChanged(object sender, EventArgs e)
		{
		}

		private void checkBox1_CheckedChanged(object sender, EventArgs e)
		{
			if (this.isIni)
			{
				if (this.checkBox1.Checked)
				{
					this.label2.Text = "相对标高(m)";
					this.comboBox1.Enabled = true;
					this.textBoxOffsetDown.Text = Math.Round(this.zCoord(double.Parse(this.textBoxOffsetDown.Text)) * 0.3048 - this.getLevelOffset(this.comboBox1.Text) * 0.3048, 3).ToString();
					return;
				}
				this.label2.Text = "绝对标高(m)";
				this.comboBox1.Enabled = false;
				this.textBoxOffsetDown.Text = Math.Round((this.zCoord(double.Parse(this.textBoxOffsetDown.Text)) + this.getLevelOffset(this.comboBox1.Text)) * 0.3048, 3).ToString();
			}
		}

		private void checkBox2_CheckedChanged(object sender, EventArgs e)
		{
			if (this.isIni)
			{
				if (this.checkBox2.Checked)
				{
					this.label4.Text = "相对标高(m)";
					this.comboBox2.Enabled = true;
					this.textBoxOffsetUp.Text = Math.Round(this.zCoord(double.Parse(this.textBoxOffsetUp.Text)) * 0.3048 - this.getLevelOffset(this.comboBox2.Text) * 0.3048, 3).ToString();
					return;
				}
				this.label4.Text = "绝对标高(m)";
				this.comboBox2.Enabled = false;
				this.textBoxOffsetUp.Text = Math.Round((this.zCoord(double.Parse(this.textBoxOffsetUp.Text)) + this.getLevelOffset(this.comboBox2.Text)) * 0.3048, 3).ToString();
			}
		}

		private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.checkBox1.Checked)
			{
				this.textBoxOffsetDown.Text = Math.Round(this.zCoord(double.Parse(this.textBoxOffsetDown.Text)) * 0.3048 + this.getLevelOffset(this.lastLevel1) * 0.3048 - this.getLevelOffset(this.comboBox1.Text) * 0.3048, 3).ToString();
				this.lastLevel1 = this.comboBox1.Text;
			}
		}

		private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.checkBox2.Checked)
			{
				this.textBoxOffsetUp.Text = Math.Round(this.zCoord(double.Parse(this.textBoxOffsetUp.Text)) * 0.3048 + this.getLevelOffset(this.lastLevel2) * 0.3048 - this.getLevelOffset(this.comboBox2.Text) * 0.3048, 3).ToString();
				this.lastLevel2 = this.comboBox2.Text;
			}
		}

		private void comboBoxPipeClass_DropDown(object sender, EventArgs e)
		{
			this.AdjustComboBoxDropDownListWidth(this.comboBoxPipeClass);
		}

		private void comboBoxPipingSystemType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.AdjustComboBoxDropDownListWidth(this.comboBoxPipingSystemType);
		}

		private void comboBox1_DropDown(object sender, EventArgs e)
		{
			this.AdjustComboBoxDropDownListWidth(this.comboBox1);
		}

		private void comboBox2_DropDown(object sender, EventArgs e)
		{
			this.AdjustComboBoxDropDownListWidth(this.comboBox2);
		}

		private void AdjustComboBoxDropDownListWidth(object comboBox)
		{
			Graphics graphics = null;
			try
			{
				System.Windows.Forms.ComboBox comboBox2 = null;
				if (comboBox is System.Windows.Forms.ComboBox)
				{
					comboBox2 = (System.Windows.Forms.ComboBox)comboBox;
				}
				else
				{
					if (!(comboBox is ToolStripComboBox))
					{
						return;
					}
					comboBox2 = ((ToolStripComboBox)comboBox).ComboBox;
				}
				int num = comboBox2.Width;
				graphics = comboBox2.CreateGraphics();
				Font font = comboBox2.Font;
				int num2 = (comboBox2.Items.Count > comboBox2.MaxDropDownItems) ? SystemInformation.VerticalScrollBarWidth : 0;
				foreach (object obj in comboBox2.Items)
				{
					if (obj != null)
					{
						int num3 = (int)graphics.MeasureString(obj.ToString().Trim(), font).Width + num2;
						if (num < num3)
						{
							num = num3;
						}
					}
				}
				comboBox2.DropDownWidth = num;
			}
			catch
			{
			}
			finally
			{
				if (graphics != null)
				{
					graphics.Dispose();
				}
			}
		}

		private Document doc;

		private List<string> levelUp = new List<string>();

		private List<string> pipeType = new List<string>();

		private List<string> pipingSystemType = new List<string>();

		public double zStart;

		public double zEnd;

		public string labelPipe;

		private Pipe pipe;

		public PipeType pipeTypeRiser;

		public PipingSystemType pipingSystemTypeRiser;

		public string diameter = "0";

		private HVACUserSetting userSetting = new HVACUserSetting();

		private OperateSegmentFitting osf;

		private bool isIni;

		public bool isFirst;

		private string lastLevel1 = "";

		private string lastLevel2 = "";
	}
}
